package com.luoqiuy.poet.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class MailUtil {

    @Value("${spring.mail.username}")
    private String sender;
    @Autowired
    private JavaMailSender javaMailSender;

    private static Logger log = LoggerFactory.getLogger(MailUtil.class);

    public boolean sendEmail(String to, String code){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sender);
        message.setTo(to);
        message.setSubject("诗园激活码");
        message.setText("欢迎注册诗园，你的注册码为:"+ code);
        try {
            javaMailSender.send(message);
            log.info("send eamil success");
            return true;
        }catch (Exception e){
            log.error("send email failed " + e.toString());
            return false;
        }
    }

    /**
     *
     * @param receiver
     * @return
     */
    public Boolean sendHtmlMail(String receiver) {
        String content = "<html><body><h3>hello world ! --->Html邮件</h3></body></html>";
        MimeMessage message = javaMailSender.createMimeMessage();

        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(receiver);
            helper.setSubject("洛秋y发送Html邮件测试");
            helper.setText(content, true);

            javaMailSender.send(message);
            log.info("send email success");
            return true;
        } catch (MessagingException e) {
            log.warn("send email failed" + e.toString());
            return false;
        }
    }


    public boolean sendFilesMail(String filePath, String receiver) {
        MimeMessage message = javaMailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(receiver);
            helper.setSubject("洛秋y发送附件邮件测试");
            helper.setText("一封带附件的邮件", true);

            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);

            javaMailSender.send(message);
            log.info("send email success");
            return true;
        } catch (MessagingException e) {
            log.warn("send email failed" + e.toString());
            return false;
        }
    }

    public boolean sendInlineResourceMail(String imgPath, String receiver) {
        String id = "xieke90";
        // cid:内嵌资源
        String content = "<html><body>带有图片的邮件：<img src=\'cid:" + id + "\'></body></html>";
        MimeMessage message = javaMailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(receiver);
            helper.setSubject("洛秋y发送图片邮件测试");
            helper.setText(content, true);

            FileSystemResource res = new FileSystemResource(new File(imgPath));
            helper.addInline(id, res);
            javaMailSender.send(message);
            log.info("send email success");
            return true;
        } catch (MessagingException e) {
            log.warn("send email failed" + e.toString());
            return false;
        }
    }
}
